@echo off
chcp 65001 >nul 2>&1
echo ==============================================
echo Onpharos Platform - Dependencies Install Script (Windows)
echo Note: For local development only. Docker auto-installs dependencies in containers.
echo ==============================================
echo.

:: Check if Python is installed
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Python not detected. Please install Python 3.8+ and configure environment variables first.
    pause
    exit /b 1
) else (
    echo [OK] Python: Detected normally
)

:: Check if pip is available
pip --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: pip not detected. Please configure Python pip environment variables first.
    pause
    exit /b 1
) else (
    echo [OK] pip: Detected normally
)

:: Check if npm is installed (for frontend)
npm --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Warning: npm not detected. Dependencies for visualizer service cannot be installed.
    echo Recommend installing Node.js 16+ first. Continue? (Y/N)
    set /p choice=
    if /i "%choice%" neq "Y" (
        exit /b 0
    )
    set npm_available=0
) else (
    echo [OK] npm: Detected normally
    set npm_available=1
)

:: 1. Install dependencies for Training Service
echo.
echo 1/4 Installing dependencies for [Training Service]...
if not exist "trainer\requirements.txt" (
    echo Error: requirements.txt not found in trainer directory!
    pause
    exit /b 1
)
cd trainer
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
if %errorlevel% equ 0 (
    echo [OK] Training Service dependencies installed successfully
) else (
    echo Error: Failed to install Training Service dependencies!
    pause
    exit /b 1
)
cd ..

:: 2. Install dependencies for Game Engine Service
echo.
echo 2/4 Installing dependencies for [Game Engine Service]...
if not exist "game_engine\requirements.txt" (
    echo Error: requirements.txt not found in game_engine directory!
    pause
    exit /b 1
)
cd game_engine
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
if %errorlevel% equ 0 (
    echo [OK] Game Engine Service dependencies installed successfully
) else (
    echo Error: Failed to install Game Engine Service dependencies!
    pause
    exit /b 1
)
cd ..

:: 3. Install dependencies for Code Generator Service
echo.
echo 3/4 Installing dependencies for [Code Generator Service]...
if not exist "code_generator\requirements.txt" (
    echo Error: requirements.txt not found in code_generator directory!
    pause
    exit /b 1
)
cd code_generator
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
if %errorlevel% equ 0 (
    echo [OK] Code Generator Service dependencies installed successfully
) else (
    echo Error: Failed to install Code Generator Service dependencies!
    pause
    exit /b 1
)
cd ..

:: 4. Install dependencies for Visualizer Service (if npm is available)
if %npm_available% equ 1 (
    echo.
    echo 4/4 Installing dependencies for [Visualizer Service]...
    if not exist "visualizer\package.json" (
        echo Error: package.json not found in visualizer directory!
        pause
        exit /b 1
    )
    cd visualizer
    npm install --registry=https://registry.npm.taobao.org
    if %errorlevel% equ 0 (
        echo [OK] Visualizer Service dependencies installed successfully
    ) else (
        echo Error: Failed to install Visualizer Service dependencies!
        pause
        exit /b 1
    )
    cd ..
)

:: All completed
echo.
echo ==============================================
echo All Dependencies Installed Successfully!
echo.
echo Local Development Commands (Reference):
echo - Start Training Service: cd trainer && uvicorn main:app --host 0.0.0.0 --port 8000
echo - Start Visualizer Service: cd visualizer && npm start
echo ==============================================
echo.
pause